package it.polimi.reTraveldream.eao;

import it.polimi.reTravelDream.dto.ExcursionDTO;
import it.polimi.reTravelDream.dto.FlightDTO;
import it.polimi.reTravelDream.dto.HotelDTO;
import it.polimi.reTravelDream.dto.TravelPackageDTO;
import it.polimi.reTravelDream.eao.PackageCreationEAOLocal;
import it.polimi.reTraveldream.entities.Excursion;
import it.polimi.reTraveldream.entities.Flight;
import it.polimi.reTraveldream.entities.Hotel;
import it.polimi.reTraveldream.entities.TravelPackage;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Session Bean implementation class PackageCreationEAO
 */
@Stateless
public class PackageCreationEAO implements PackageCreationEAOLocal{

	@PersistenceContext
	private EntityManager em;
	
	//private HotelEAO hotelEAO;
	
    public PackageCreationEAO() {
        // TODO Auto-generated constructor stub
    }

	@Override
	public void createNewPackage(TravelPackageDTO travelPackageDTO) {
		System.out.println("package creation EAO"+travelPackageDTO.getPackageExcursions());
		
		List<Flight> flightsToAdd=new ArrayList<Flight>();
		List<Hotel> hotelToAdd=new ArrayList<Hotel>();
		List<Excursion> excursionToAdd=new ArrayList<Excursion>();
		
		List<HotelDTO> hotelList=new ArrayList<HotelDTO>();
		List<FlightDTO> flightList=new ArrayList<FlightDTO>();
		List<ExcursionDTO> excursionList=new ArrayList<ExcursionDTO>();
		
		hotelList=travelPackageDTO.getPackageHotelDTOs();
		flightList=travelPackageDTO.getPackageFlights();
		excursionList=travelPackageDTO.getPackageExcursions();
		
		for(HotelDTO hDto:hotelList){
			
			hotelToAdd.add(em.find(Hotel.class, hDto.getId()));
		}
		
		for(FlightDTO fDto:flightList){
			flightsToAdd.add(em.find(Flight.class, fDto.getId()));
		}
		for(ExcursionDTO eDto:excursionList){
			excursionToAdd.add(em.find(Excursion.class, eDto.getId()));
		}
		
		
		
		TravelPackage  tPackage= new TravelPackage(travelPackageDTO,flightsToAdd,hotelToAdd,excursionToAdd);
		em.persist(tPackage);
	}

	



	

}
